"use strict";

const Sequelize = require("sequelize");

module.exports = (app) => {
  const { STRING, INTEGER, DATE } = Sequelize;
  const ChatModel = app.model.define(
    "chat",
    {
      id: {
        type: INTEGER,
        primaryKey: true,
        autoIncrement: true,
        comments: "唯一标识符",
      },
      from: {
        type: INTEGER,
        allowNull: false,
        comments: "用户id",
        references: {
          model: app.model.UsersModel,
          key: "id",
        },
      },
      to: {
        type: INTEGER,
        allowNull: false,
        comments: "用户id",
        references: {
          model: app.model.UsersModel,
          key: "id",
        },
      },
      content: {
        type: STRING,
        allowNull: false,
        comments: "内容",
      },
      created_at: {
        type: DATE,
      },
    },
    {
      tableName: "chat",
    }
  );

  ChatModel.associate = function () {
    ChatModel.belongsTo(app.model.UsersModel, {
      foreignKey: "from",
      as: "sender",
    });

    ChatModel.belongsTo(app.model.UsersModel, {
      foreignKey: "to",
      as: "receiver",
    });
  };

  return ChatModel;
};
